package yunplus.io.hchome.model.api.implement;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.company.PlaySDK.Constants;
import com.company.PlaySDK.IPlaySDK;
import com.company.PlaySDK.IPlaySDKCallBack;

import java.io.FileInputStream;

import yunplus.io.hchome.display.view.IVedioView;
import yunplus.io.hchome.model.api.contract.IVedioModel;

import static android.R.attr.mode;
import static android.R.attr.port;

/**
 * Created by cruze on 2017/6/7.
 */

public class VedioModel implements IVedioModel {

    private static final int FILE = 0;
    private static final int FILESTREAM = 1;
    private static final int FILE_PATH = 101; // 文件路径

    private static final String curfile = "101"; // 文件路径


    private static VedioModel vedioModel = null;

    private IVedioView iVedioView;

    public static VedioModel getInstance(Context context,IVedioView iview) {
        if (vedioModel == null) {
            vedioModel = new VedioModel(context,iview);
        }
        return vedioModel;
    }

    private VedioModel(Context context,IVedioView iview){
        this.iVedioView = iview;
    }


    @Override
    public void startRecord() {
        int retValue = 0;
        if (mode == FILE) {
            long lUsrdata = 0;
            retValue = IPlaySDK.PLAYSetFileEndCallBack(port, new PlayEndCallBack(), lUsrdata);
            if (0 == retValue) {
                return;
            }

            retValue = IPlaySDK.PLAYOpenFile(port, curfile);
            if (0 == retValue) {
                return;
            }
        } else {
            IPlaySDK.PLAYSetStreamOpenMode(port, Constants.STREAME_FILE);
            retValue = IPlaySDK.PLAYOpenStream(port, null, 0, 5 * 1024 * 1024);
            if (0 == retValue) {
                return;
            }
        }
        retValue = iVedioView.play();

        if (0 == retValue) {
            return;
        }

        retValue = IPlaySDK.PLAYPlaySound(port);
        if (0 == retValue) {
            Log.d("[playsdk]", "PLAYPlaySound Failed.");
        }

        //sound capture test.
        int m_nBitPerSample = 16;
        int m_nSamplePerSecond = 8000;
        int lSampleLen = m_nBitPerSample * m_nSamplePerSecond / 8;
        TestAudioRecord m_audiorecordcallback = new TestAudioRecord();
        retValue = IPlaySDK.PLAYOpenAudioRecord(m_audiorecordcallback, m_nBitPerSample, m_nSamplePerSecond, lSampleLen, 0);
        if (0 == retValue) {
            Log.d("[playsdk]", "PLAYOpenAudioRecord Failed.");
        }

        if (mode == FILESTREAM) {
            try {
                FileInputStream fis = new FileInputStream(curfile);
                int readsize = 1024 * 1024;
                byte[] buffer = new byte[readsize];
                int readlen = 0;
                int ret = 0;
                while ((readlen = fis.read(buffer)) != -1) {
                    ret = IPlaySDK.PLAYInputData(port, buffer, readlen);
                    if (ret == 0) {
                        Log.d("[playsdk]PLAYInputData", "Failed.");
                    }
                }

                fis.close();
            } catch (Exception e) {
            }
        }
    }

    @Override
    public void pauseRecord() {

    }

    @Override
    public void stopRecord() {
        IPlaySDK.PLAYStopSound();
        //clear Screen
        IPlaySDK.PLAYCleanScreen(port, 0/*red*/, 0/*green*/, 0/*blue*/, 1/*alpha*/, 0);
        IPlaySDK.PLAYStop(port);
        IPlaySDK.PLAYCloseAudioRecord();

        if (mode == FILE) {
            IPlaySDK.PLAYCloseFile(port);
        } else {
            IPlaySDK.PLAYCloseStream(port);
        }
    }

    @Override
    public void play() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void seek(boolean forward, double progress) {

    }

    @Override
    public void next() {

    }

    @Override
    public void prev() {

    }


    private class PlayEndCallBack implements IPlaySDKCallBack.fpFileEndCBFun {
        @Override
        public void invoke(int nPort, long pUserData) {
            try {
                Message msg = new Message();
                msg.what = 2;
                handler.sendMessage(msg);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            if (msg.what == 1) {
                if (mode == FILE) {
                    float fproc = IPlaySDK.PLAYGetPlayPos(port);
                    iVedioView.updateProcess(fproc * 100);
                } else {
                    //if mode is filestream
                    //need to compute the rate of have readed size in the total file
                }
            } else if (msg.what == 2) {
                stopRecord();
            }
        }

        ;
    };

    public class TestAudioRecord implements IPlaySDKCallBack.pCallFunction {
        public void invoke(byte[] pDataBuffer, int nBufferLen, long pUserData) {
            try {
                /*
                //sound capture test.
				File file = new File("/mnt/sdcard/test/audiorecord.pcm");
				FileOutputStream fout = new FileOutputStream(file, true);
				fout.write(pDataBuffer);
				fout.close();
				*/
            } catch (Exception e) {
            }
        }

    }


}
